package com.tofine.smarthome.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

import com.google.gson.Gson;
import com.tofine.smarthome.rest.model.Command;

import cn.bmob.push.PushConstants;

/**
 * @author ddMax
 * @since 2016/5/28 21:15.
 */
public class DeviceCommandReceiver extends BroadcastReceiver {

    private CommandListener mCommandListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(PushConstants.ACTION_MESSAGE)){
            Gson gson = new Gson();
            Command command = gson.fromJson(intent.getStringExtra(PushConstants.EXTRA_PUSH_MESSAGE_STRING), Command.class);
            mCommandListener.onCommandReceived(command);
        }
    }

    public interface CommandListener {
        public void onCommandReceived(Command command);
    }

    public void setOnReceivedCommandListener(CommandListener commandListener) {
        this.mCommandListener = commandListener;
    }

}
